<template>
  <!-- 产品系列表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    style="min-width: 600px; max-width: 800px"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      status-icon
      label-width="120px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品系列编号" prop="productSeriesCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.productSeriesCode"
              placeholder="请输入产品系列编号"
              clearable
            />
            <el-tooltip
              v-else
              :content="formData.productSeriesCode"
              effect="light"
            >
              <span class="word">{{ formData.productSeriesCode }}</span>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="产品系列名称" prop="productSeriesName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.productSeriesName"
              placeholder="请输入产品系列名称"
              clearable
            />
            <el-tooltip
              v-else
              :content="formData.productSeriesName"
              effect="light"
            >
              <span class="word">{{ formData.productSeriesName }}</span>
            </el-tooltip>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="产品平台" prop="platformId">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.platformId"
              placeholder="请输入产品平台"
              style="width: 300px"
              filterable
            >
              <el-option
                v-for="item in platformNameOptions"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-tooltip
              v-else
              :content="platformNameOptionsName(formData.platformId)"
              effect="light"
            >
              <span class="word">{{
                platformNameOptionsName(formData.platformId)
              }}</span>
            </el-tooltip>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态" prop="enabled">
            <el-radio-group v-if="!props.isReadonly" v-model="formData.enabled">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
            <span v-else>{{ formData.enabled === 1 ? "启用" : "停用" }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <el-form-item v-if="props.isReadonly" label="产品平台编号">
            <span>{{ formData.platformId }}</span>
          </el-form-item>
        </el-col>
      </el-row> -->
      <el-row v-show="props.isReadonly">
        <el-col :span="12">
          <el-form-item
            label="创建时间"
            v-if="formData.id && !props.isReadonly"
            prop="createTime"
          >
            <el-input v-model="formData.createTime" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="创建时间">
            <span>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="创建人"
            v-if="formData.id && !props.isReadonly"
            prop="createdUserName"
          >
            <el-input v-model="formData.createdUserName" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="创建人">
            <span>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-show="props.isReadonly">
        <el-col :span="12">
          <el-form-item
            label="更新时间"
            v-if="formData.id && !props.isReadonly"
          >
            <el-input v-model="formData.lastUpdateTime" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="更新时间">
            <span>{{ formData.lastUpdateTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="更新人" v-if="formData.id && !props.isReadonly">
            <el-input v-model="formData.lastUpdatedUserName" disabled />
          </el-form-item>
          <el-form-item v-if="props.isReadonly" label="更新人">
            <span>{{ formData.lastUpdatedUserName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMMDMPRODUCTSERIESADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  platformNameOptions: {
    type: Object,
    default: () => {
      return [];
    },
  },
});

const platformNameOptionsName = (platformId: string) => {
  return props.platformNameOptions.find(
    (item: any) => item.value === platformId
  )?.label;
};

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  productSeriesCode: [
    { required: true, message: "请输入产品系列编号", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productSeriesName: [
    { required: true, message: "请输入产品系列名称", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  platformId: [
    { required: true, message: "请输入产品平台", trigger: "change" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  productSeriesCode: "",
  productSeriesName: "",
  platformId: "",
  enabled: 1,
  remarks: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        productSeriesCode,
        productSeriesName,
        platformId,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        productSeriesCode,
        productSeriesName,
        platformId,
        enabled,
        remarks,
        deleted,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formData.id = "";
  formData.version = "";
  formData.productSeriesCode = "";
  formData.productSeriesName = "";
  formData.platformId = "";
  formData.enabled = 1;
  formData.remarks = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 产品系列保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/mdmproductseries/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>

<style scoped lang="scss">
h1 {
  margin: 0 10px 0 0;
  font-weight: bold;
  font-size: 16px;
}

.word {
  word-wrap: break-word;
  width: 180px;
  /* 设置容器宽度 */
}
</style>
